package com.summer.peony.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.summer.peony.common.entity.AjaxResult;
import com.summer.peony.common.entity.AjaxResult.Type;
import com.summer.peony.system.pojo.SysDept;
import com.summer.peony.system.service.SysDeptService;

/**
 * @ClassName: DeptController
 * @Description: TODO(部门管理控制层)
 * @author: TaoGe
 * @date: 2020年6月9日 下午3:50:34
 * @version: v0.0.1
 */
@RestController
@RequestMapping("/system")
public class SysDeptController {

	@Autowired
	private SysDeptService sysDeptService;

	/**
	 * @Description: TODO(新增部门)
	 * @param sysDept
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年6月24日 下午4:21:54
	 * @throws:
	 */
	@PostMapping("/addDept")
	public AjaxResult sysDeptAdd(@RequestBody SysDept sysDept) {
		AjaxResult ajaxResult = new AjaxResult();
		try {
			int insert = sysDeptService.insert(sysDept);
			if (insert == 1) {
				ajaxResult.put("code", 0);
				ajaxResult.put("msg", "添加成功");
				return ajaxResult;
			}
			ajaxResult.put("code", 900001);
			ajaxResult.put("msg", "部门编码已存在");
			return ajaxResult;
		} catch (Exception e) {
			e.printStackTrace();
			ajaxResult.put("code", 999999);
			ajaxResult.put("msg", "添加失败");
			return ajaxResult;
		}
	}

	/**
	 * @Description: TODO(更新部门信息)
	 * @param sysDept
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年6月24日 下午4:57:25
	 * @throws:
	 */
	@PutMapping("/updateDept")
	public AjaxResult updateDept(@RequestBody SysDept sysDept) {
		int upd = sysDeptService.updateByPrimaryKey(sysDept);
		AjaxResult ajaxResult = new AjaxResult();
		if (upd == 1) {
			ajaxResult.put("code", 0);
			ajaxResult.put("msg", "修改成功");
			return ajaxResult;
		}
		ajaxResult.put("code", 900002);
		ajaxResult.put("msg", "部门编码已存在");
		return ajaxResult;
	}

	/**
	 * @Description: TODO(删除部门信息)
	 * @param deptId
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年6月24日 下午4:21:41
	 * @throws:
	 */
	@DeleteMapping("/delDept/{deptId}")
	public AjaxResult delDept(@PathVariable("deptId") Integer deptId) {
		int del = sysDeptService.deleteByPrimaryKey(deptId);
		if (del == 1) {
			return new AjaxResult(Type.SUCCESS, "删除成功");
		}
		if (del == 2) {
			return new AjaxResult(Type.ERROR, "有下级部门不可删除");
		}
		return new AjaxResult(Type.ERROR, "删除失败");
	}

	/**
	 * @Description: TODO(根据名称、编码搜索)
	 * @param name
	 * @param code
	 * @param pageNum
	 * @param pageSize
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年9月22日 下午2:57:55
	 * @throws:
	 */
	@GetMapping("/selectAllDepts")
	public AjaxResult byNameDept(String name, String code, Integer pageNum, Integer pageSize) {
		PageInfo<SysDept> selectAllDepts = sysDeptService.selectByNameDept(name, code, pageNum, pageSize);
		if (selectAllDepts != null) {
			return new AjaxResult(Type.SUCCESS, "查询部门列表", selectAllDepts);
		}
		return new AjaxResult(Type.SUCCESS, "数据为空", selectAllDepts);
	}

	/**
	 * @Description: TODO(查询部门列表)
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月29日 上午9:18:30
	 * @throws:
	 */
	@GetMapping("/allDepts")
	public AjaxResult selectAllDept() {
		List<SysDept> list = sysDeptService.selectAllDepts();
		if (!list.isEmpty()) {
			return new AjaxResult(Type.SUCCESS, "查询部门列表", list);
		}
		return new AjaxResult(Type.SUCCESS, "查询数据为空");
	}

	/**
	 * @Description: TODO(查询单个部门信息)
	 * @param deptId
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年6月24日 下午4:20:57
	 * @throws:
	 */
	@GetMapping("/byIdDept/{deptId}")
	public AjaxResult selectDept(@PathVariable("deptId") Integer deptId) {
		SysDept sysDept = sysDeptService.selectByPrimaryKey(deptId);
		if (sysDept != null) {
			return new AjaxResult(Type.SUCCESS, "查询成功", sysDept);
		}
		return new AjaxResult(Type.SUCCESS, "部门不存在");
	}

	/**
	 * @Description: TODO(查询部门层级列表)
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月29日 上午9:18:55
	 * @throws:
	 */
	@GetMapping("/allSysDepts")
	public AjaxResult allSysDepts() {
		List<SysDept> allSysDepts = sysDeptService.allSysDepts();
		if (allSysDepts != null) {
			return new AjaxResult(Type.SUCCESS, "查询成功", allSysDepts);
		}
		return new AjaxResult(Type.ERROR, "查询失败");
	}
}
